#include <gtkmm-2.4/gtkmm.h>
#include <glibmm-2.4/glibmm.h>
#include <gdkmm-2.4/gdkmm.h>
#include <gdkmm-2.4/gdkmm/pixbuf.h>
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include "catImage.cpp"
using namespace std;

class iList 
{
	protected:
		vector<catImage> imageList;
		Gtk::Image currentImage;
		Gtk::ScrolledWindow imageScroll;
		Gtk::Window previewWindow;
		int cindex;
		int pageNumber;
		Gtk::Table thumbnailTable;
		Gtk::Image* tempThumb;

		bool jumpToImage(int nindex)
		{
			cindex = nindex;
			return true;

		}

		virtual bool tClick(GdkEventButton *event)
		{
			cout<<"Image object clicked!"<<endl;

			return true;
		}

		void displayImage()
		{
			Glib::RefPtr<Gdk::Pixbuf> pb = imageList[cindex].
							getPixbuf();

			currentImage.set(pb);
		}

	public:
		iList(vector<catImage>* imagevector) : cindex(0)
		{
			imageList = *imagevector;
			imageScroll.set_size_request(800,600);
			imageScroll.set_policy(Gtk::POLICY_AUTOMATIC,
					Gtk::POLICY_AUTOMATIC);
		}

		iList(vector<Glib::ustring>* fnL,Glib::ustring path) : cindex(0)
		{
			vector<Glib::ustring> fnList = *fnL;
			for(register int x=0; x++<fnList.size()-1;)
			{
				if(catImage::checkType(fnList[x],path))
				{
					imageList.
					push_back(catImage(fnList[x],path));
				}
			}

		}

		void goNext()
		{
			if(cindex >= imageList.size()-1)
			{
				cindex = 0;
			}
			else
			{
				cindex++;
			}

			displayImage();
		}

		void goBack()
		{
			if(cindex==0)
			{
				cindex = imageList.size()-1;
			}
			else
			{
				cindex--;
			}

			displayImage();
		}

		void refresh()
		{
			displayImage();
		}

		Gtk::ScrolledWindow* getScrolledWindow()
		{
			return &imageScroll;
		}


		/*
		 * 
		 * This function suffers from some serious
		 * memory leakage.  Some thought needs to
		 * be put into the best way to free up the
		 * memory allocated in this class- it may
		 * involve once again going back to a 2 class
		 * model for doing the preview and scroll windows.
		 *
		 * Most likely we can just keep a pointer the first
		 * address of teb and tempThumb.  That should work out.
		 *
		 */
		Gtk::Window* getPreviewWindow(int rows, int cols)
		{
			Gtk::Button nextPage("Next Page");
			Gtk::Button backPage("Back Page");
			Gtk::VBox wpk;
			Gtk::EventBox* teb;
			int pivot = pageNumber*(rows*cols);
		       	int counter=pivot;
			for(int x=0;x<rows && counter<imageList.size();x++)
			{
				for(int y=0;y++<cols&&counter<imageList.size();)
				{
					teb = new Gtk::EventBox;

					imageList[counter].fitInto(128,128);

					tempThumb = new Gtk::Image(
							imageList[counter].
							getScaledInstance());

					teb->add(*tempThumb);
					teb->set_events(Gdk::BUTTON_PRESS_MASK);
					teb->signal_button_press_event()
							.connect(
							sigc::mem_fun(
							*this,&iList::tClick));
					thumbnailTable.attach(*teb,
							guint(y),guint(y+1),
							guint(x),guint(x+1),
							Gtk::EXPAND,
							Gtk::EXPAND,
							guint(3),
							guint(3));

					tempThumb->show();
					teb->show();
					teb++;
					tempThumb++;
				}
			}
			wpk.pack_start(thumbnailTable);
			wpk.pack_start(nextPage);
			nextPage.show();
			previewWindow.add(wpk);
			thumbnailTable.show();
			previewWindow.show();
			return &previewWindow;
		}
};
